﻿using System;
using System.Collections.Generic;
using System.Text;

using AGG.Font;
using AGG.Transform;

namespace AGG.UI
{
    public class TabControlWidget : GUIWidget
    {
		class TabBar : GUIWidget
		{
			public override void OnDraw (Graphics2D graphics2D)
			{
				graphics2D.Rect(Bounds, new RGBA_Bytes(0, 0, 255, 128));
				base.OnDraw (graphics2D);
			}
		}
		
		class TabPageArea : GUIWidget
		{
			public override void OnDraw (Graphics2D graphics2D)
			{
				graphics2D.Rect (Bounds, new RGBA_Bytes (255, 0, 0, 128));
				base.OnDraw (graphics2D);
			}
		}
		
        TabPageCollection tabPages = new TabPageCollection();

        StyledTypeFace typeFaceStyle = new StyledTypeFace(LiberationSansFont.Instance, 12);
		TabBar tabBar;
		TabPageArea tabPageArea;

        public TabPageCollection TabPages 
		{
			get 
			{
				return tabPages;
			}
		}
		
		public override rect_d Bounds {
			get {
				return base.Bounds;
			}
			set {
				base.Bounds = value;
				if(tabBar != null)
				{
					tabBar.Bounds = new rect_d(Bounds.Left, Bounds.Top - 30, Bounds.Right, Bounds.Top);
					tabPageArea.Bounds = new rect_d(Bounds.Left, Bounds.Bottom, Bounds.Right, Bounds.Top - 30);
				}
			}
		}
		
		public override void OnParentChanged ()
		{
			DockToEdge (AnchorFlags.All);
			
			tabBar = new TabBar ();
			this.AddChild (tabBar);
			tabBar.Bounds = new rect_d (0, Bounds.Top - 30, Bounds.Right, Bounds.Top);
			tabPageArea = new TabPageArea ();
			this.AddChild (tabPageArea);
			tabPageArea.Bounds = new rect_d (0, 0, Bounds.Right, Bounds.Top - 30);
			
			double tabPosition = 0;
			
			foreach (KeyValuePair<String, TabPageWidget> page in tabPages) {
				TextWidget tabName = new TextWidget (page.Value.Text, 12);
				tabName.Transform = Affine.NewTranslation (tabPosition, 0);
				tabPosition += tabName.Bounds.Width;
				tabBar.AddChild (tabName);
			}			
			
			base.OnParentChanged ();
		}
		
        public override void OnDraw(Graphics2D graphics2D)
        {
            base.OnDraw(graphics2D);
        }
    }
}
